// room2d图类
	
/**
 * 构造方法
 * @param _data room图配置数据
 * @param _layout room图布局对象，不传就为默认布局
 */
var Room = function(_data, _layout) {
	
	// --------------------------------------------------------------------------
    // import
    // --------------------------------------------------------------------------
    
    const BaseChart = ghca_charts.view.component.charts.baseChart;
    const ObjectUtil =  ghca_charts.view.util.objectUtil;
    const ClassFactory = ghca_charts.view.util.classFactory;

    BaseChart.apply(this, arguments);

    var self = this,
        superInit = self.init,
        superInitAllContainers = self.initAllContainers,
        superAddListeners = self.addListeners,
        superRefreshData = self.refreshData,
        superRenderPosition = self.renderPosition;

    self.className("room");
    
    var isInit = true;

    // --------------------------------------------------------------------------
    //
    // Attributes
    //
    // --------------------------------------------------------------------------
    
    // ------------------------------
    // nodeInsMap
    // ------------------------------
    
    /**
     * 节点实例map
     */
    var nodeInsMap = d3.map();
    this.nodeInsMap = function(){
        return nodeInsMap;
    };
    
    // --------------------------------------------------------------------------
    //
    // override Methods
    //
    // --------------------------------------------------------------------------
        
    /**
     * 初始化方法
     */
    this.init = function() {
        superInit();
    };

    /**
     * 初始化图表包含的各个层级
     */
    this.initAllContainers = function() {
        superInitAllContainers();
        self.roomContainer =  self.mainContainer().append("g").attr("class", "room-Container");
        //放置所有节点的g元素
        self.nodesContainer =  self.roomContainer.append("g").attr("class", "nodes-Container");
    };

    /**
     * 初始化添加所有监听的方法
     */
    this.addListeners = function() {
        superAddListeners();
    };
    
    /**
     * 刷新数据内容，生成需要的属性和相关值
     */
   this.refreshData = function() {
        superRefreshData();
        
        var items = self.data().data;
        for(var item, i = 0, len = items.length; i < len; i++) {
            item = items[i];
            if(!item.hasOwnProperty("id")) {
                item.id = i;
            }
            //生成visible属性，默认值为true
            if(!item.hasOwnProperty("visible")) {
                item.visible = true;
            }
        };
    };

    /**
     * 渲染图表内容相关位置
     */
    this.renderPosition = function(transition) {
        superRenderPosition();
        
        var nodeInsMap = self.nodeInsMap();
        var nodeIns, i = 0;;
        for(var key in nodeInsMap) {
            nodeIns = nodeInsMap[key];
            isInit && transition.delay(i * 0);
            nodeIns.el && nodeIns.el.transition(transition)
                .attr("transform", function(d) {
                    return  "translate(" + d.x + "," + d.y + ")";
                })
                .attr("opacity", 1);
            i++;
        }
    };
    
    /**
     * 根据当前数据重新渲染内容，并重新计算布局内容
     */
    this.update = function() {
    	self.refreshData();
    	
        if(!self.data() ||　!ObjectUtil.isArray(self.data().data) || self.data().data.length === 0) {
            return;
        }

        var visibleItems = self.filterArrayByElementProperty(self.data().data, "visible", true);
        
        //render items
        var roomItemsUpdate = self.nodesContainer.selectAll("g.room-item")
            .data(visibleItems, function(d) {
                return d.id;
            });
        //delete
        roomItemsUpdate.exit().each(function(d) {
            var id = d.id;
            var main = self.mainContainer();
            var ins = self.nodeInsMap().get(id);
            self.nodeInsMap().remove(id);
            d3.select(this).remove();
        });
        //update
        roomItemsUpdate.each(function(d) {
            var ins = self.nodeInsMap().get(d.id);
            if(ins) {
//            	var rendererConfig = self.getRendererConfig(d, "fill");
//                var clazz = (rendererConfig && rendererConfig.clazz) ? rendererConfig.clazz : "node";
//                ins.config(rendererConfig.config);
                ins.update(ins.el);
            }
        });
        //add
       roomItemsUpdate.enter().append("g")
            .attr("class", "room-item")
            .each(function(d){
                var rendererConfig = self.getRendererConfig(d, "fill");
                var clazz = (rendererConfig && rendererConfig.clazz) ? rendererConfig.clazz : "node";
                var nodeIns = ClassFactory.newNodeInstance(clazz, [d3.select(this), d, self]);
                nodeIns.config(rendererConfig.config);
                if(nodeIns.hasOwnProperty("ownerData")) {
                    nodeIns.ownerData(self.data());
                }
                nodeIns.isChartFirstInit(isInit);
                nodeIns.graph(self.graph());
                nodeIns.render();
                self.nodeInsMap().set(d.id, nodeIns);
                d.legendIns = d3.select(this);
                nodeIns.el
                    .attr("transform", "translate(" + self.width()/2 + "," + d.y + ")")
                    .attr("opacity", 0);
            });

        self.renderPosition(isInit ? d3.transition().duration(500) : d3.transition().duration(0));
        
        isInit = false;
    };
    
    // --------------------------------------------------------------------------
    //
    // listeners
    //
    // --------------------------------------------------------------------------
    
    
    // --------------------------------------------------------------------------
    //
    // private Methods
    //
    // --------------------------------------------------------------------------
    
    Room.clazzName = "room";
    
    // 初始化
    this.init();
};

Room.struct = function(room2d) {
	var BaseChart = ghca_charts.view.component.charts.baseChart;
	BaseChart.struct(ghca_charts);
    Room.prototype = Object.create(BaseChart.prototype);
    Room.prototype.constructor = Room;
};

module.exports = Room;